<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0, user-scalable=0">
		<title>发表评论</title>
		<link href="../AmazeUI-2.4.2/assets/css/admin.css" rel="stylesheet" type="text/css">
		<link href="../AmazeUI-2.4.2/assets/css/amazeui.css" rel="stylesheet" type="text/css">
		<link href="../css/personal.css" rel="stylesheet" type="text/css">
		<link href="../css/appstyle.css" rel="stylesheet" type="text/css">
		<script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
		<script type="text/javascript" src="../js/common.js"></script>
		<script type="text/javascript" src="../js/sweetalert2.all.js"></script>
	</head>

	<body>
		<header>
			<article>
				<div class="mt-logo">
					<!--顶部导航条 -->
					<div class="am-container header">
						<ul class="message-l">
							<div class="topMessage">
								<div class="menu-hd" id="indexHeader">
								</div>
							</div>
						</ul>
						<ul class="message-r">
							<div class="topMessage home">
								<div class="menu-hd">
									<a href="index.html" target="_top" class="h">商城首页</a>
								</div>
							</div>
						</ul>
					</div>
					<div class="nav white">
					</div>

					<div class="clear"></div>
				</div>
				</div>
			</article>
		</header>
		<div class="nav-table">
			<div class="long-title"><span class="all-goods">个人中心</span></div>
			<div class="nav-cont">
				<ul>
				</ul>
			</div>
		</div>
		<b class="line"></b>
		<div class="center">
			<div class="col-main">
				<div class="main-wrap">

					<div class="user-comment">
						<!--标题 -->
						<div class="am-cf am-padding">
							<div class="am-fl am-cf"><strong class="am-text-danger am-text-lg">发表评论</strong> / <small>Make&nbsp;Comments</small></div>
						</div>
						<hr/>

						<div class="comment-main">
							

							
							<script type="text/javascript">
								$(document).ready(function() {
									$(".comment-list .item-opinion li").click(function() {
										$(this).prevAll().children('i').removeClass("active");
										$(this).nextAll().children('i').removeClass("active");
										$(this).children('i').addClass("active");
									});
								})
							</script>

						</div>
						<div class="info-btn">
								<div class="am-btn am-btn-danger" onclick="submitComments()">发表评论</div>
							</div>
					</div>
				</div>
			</div>

			<!--侧边栏-->
			<aside class="menu">
				<ul>
					<li class="person active">
						<a href="pindex.html"><i class="am-icon-user"></i>个人中心</a>
					</li>
					<li class="person">
						<p><i class="am-icon-newspaper-o"></i>个人资料</p>
						<ul>
							<li>
								<a href="pinformation.html">个人信息</a>
							</li>
						</ul>
					</li>
					<li class="person">
						<p><i class="am-icon-balance-scale"></i>我的交易</p>
						<ul>
							<li>
								<a href="order.html">订单管理</a>
							</li>
						</ul>
					</li>
				</ul>
			</aside>
		</div>
		<script>
			function getQueryStringByName(name) {
		        var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
		        if (result == null || result.length < 1) {
		            return "";
		        }
		        return result[1];
		    }
		</script>
		
		<script>
			$(function(){
				/*检验用户是否登录*/
				if (getCookie("username") == null || getCookie("userid") == null) {
                   window.location.href="login.html";
                   return;
			    }else{
			    	$("#indexHeader").append(`
                         <a href="#" target="_top" class="h">您好，${getCookie("username")}</a>
                          <a href="javascript:void(0);" onclick="clearCookie()">退出</a>`
                    );
			    }
			    
			    var orderid=getQueryStringByName("orderid");
			    /*根据订单号查询订单详情*/
			    $.ajax({
			    	type:"get",
			    	url:"/orderdetail/selectByOrderid/"+orderid,
			    	async:true,
			    	success:function(result){
			    		for(let i=0;i<result.length;i++){
			    			//查询商品信息
							var goods;
							$.ajax({
				   				type:"get",
				   				url:"/goods/info/"+result[i].goodsid,
				   				async:false,
				   				success:function(rs){
				   					goods=rs.data;
				   				}
			   				});
			   				
			    			var html=`
			    			<div class="comment-list">
								<div class="item-pic">
									<a href="#" class="J_MakePoint">
										<img src="${goods.img1}" class="itempic">
									</a>
								</div>
								<div class="item-title">
									<div class="item-name">
										<a href="#">
											<p class="item-basic-info">${goods.goodsname}</p>
										</a>
									</div>
									<div class="item-info">
										<div class="item-price">
											价格：<strong>${goods.price}元</strong>
										</div>
									</div>
								</div>
								<div class="item-comment">
									<textarea name="tt-${goods.id}" placeholder="请写下对宝贝的感受吧，对他人帮助很大哦！"></textarea>
								</div>
							</div>`;
							
							$(".comment-main").append(html);
			    		}
			    	}
			    });
			})
			
			
			/*提交评论*/
			function submitComments(){
				//用户id
				var userid=getCookie("userid");
				
				var isStopSubmit=true;
				$("textarea").each(function(){
					//评论的内容
                    let content=$(this).val();
                    //判断是否有空内容
                    if(content==null||content==''){
                    	isStopSubmit=false;
                    }
				});
				if(!isStopSubmit){
					swal({
                        title: '请完整填写后提交',
                        type: 'error',
                        showConfirmButton: false,
                        timer: 1500
                    });
					return;
				}
				
				//更新订单状态
                var d={"id":getQueryStringByName("orderid"),"status":3};
                $.ajax({
                    type:"post",
                    url:"/orders/update",
                    data:JSON.stringify(d),
                    async:false,
                    contentType:"application/json; charset=utf-8"
                });
				
				$("textarea").each(function(){
					//通过textarea的name解析得到评论对应的商品id
					let ttname=$(this).attr("name");
					let goodsid=ttname.split("-")[1];
					
					//评论的内容
					let content=$(this).val();
					
					//组装数据
					let data={"content":content,"createUserid":userid,"goodsid":goodsid};
					
					//提交数据
					$.ajax({
						type:"post",
						url:"/message/insert",
						data: JSON.stringify(data),
						contentType: "application/json; charset=utf-8",
						dataType: "json",
						success:function(){
							swal({
									title: '评论成功',
									type: 'success',
									showConfirmButton: false,
									timer: 1500
								}).then(
								    function (){
								        window.location.href="order.html";
								    }
								);
						},
						error:function(){
							swal({
									title: '抱歉，评论失败',
									type: 'error',
									showConfirmButton: false,
									timer: 1500
								})
						}
					});
				});
			}
		</script>
	</body>

</html>